//
//  MOTAutoSizeLabel.m
//  MOTAutoSizeLabel
//
//  Created by 怪兽K on 16/8/24.
//  Copyright © 2016年 jiyun. All rights reserved.
//

#import "MOTAutoSizeLabel.h"

@interface MOTAutoSizeLabel ()
{
    CGFloat _strWidth;
    CGFloat _strHeight;
}

@end

@implementation MOTAutoSizeLabel

#pragma mark -系统方法
-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}
-(instancetype)init {
    if (self = [super init]) {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}
-(instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

-(void)setText:(NSString *)text {
    _text = text;
    
    if (_isAutoWidth) {
        self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, [self getWidth], self.frame.size.height);
        
        if (_delegate) {
            [_delegate changeWidth:[self getWidth]];
        }
    }
    if (_isAutoHeight) {
        self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, [self getHeight]);
        
        if (_delegate) {
            [_delegate changeHeight:[self getHeight]];
        }
    }
    
    [self setNeedsDisplay];
}
-(void)setTextInset:(UIEdgeInsets)textInset {
    _textInset = textInset;
    [self setNeedsDisplay];
}
-(void)setFrame:(CGRect)frame {
    [super setFrame:frame];
    [self setNeedsDisplay];
}
-(void)setCenter:(CGPoint)center {
    [super setCenter:center];
    [self setNeedsDisplay];
}
-(void)setFont:(UIFont *)font {
    _font = font;
    [self setNeedsDisplay];
}
-(void)setTextColor:(UIColor *)textColor {
    _textColor = textColor;
    [self setNeedsDisplay];
}
-(void)setIsAutoWidth:(BOOL)isAutoWidth {
    _isAutoWidth = isAutoWidth;
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, [self getWidth], self.frame.size.height);
    self.layer.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, [self getWidth], self.frame.size.height);
}
-(void)setIsAutoHeight:(BOOL)isAutoHeight {
    _isAutoHeight = isAutoHeight;
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, [self getHeight]);
}

-(void)drawRect:(CGRect)rect {
    
    //设置字符串的显示属性
    NSDictionary *attrDic = @{NSFontAttributeName: self.font ? self.font : [UIFont systemFontOfSize:13], NSForegroundColorAttributeName:self.textColor ? self.textColor : [UIColor blackColor]};
    
    //根据设定的最大宽度或者高度，来计算出字符串需要占用的空间大小
    CGRect strRect = [self.text boundingRectWithSize:CGSizeMake(self.isAutoWidth ? MAXFLOAT : self.frame.size.width, self.isAutoHeight ? MAXFLOAT : self.frame.size.height) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:attrDic context:nil];
    _strWidth = strRect.size.width;
    _strHeight = strRect.size.height;
    
    [self.text drawInRect:CGRectMake(self.textInset.left, self.textInset.top, strRect.size.width - self.textInset.left - self.textInset.right, strRect.size.height - self.textInset.top - self.textInset.bottom) withAttributes:attrDic];
}

-(CGFloat)textWidth {
    return [self getWidth];
}
-(CGFloat)textHeight {
    return [self getHeight];
}

#pragma mark -私有方法
-(CGFloat)getWidth {
    NSDictionary *attrDic = @{NSFontAttributeName: self.font ? self.font : [UIFont systemFontOfSize:13], NSForegroundColorAttributeName:self.textColor ? self.textColor : [UIColor blackColor]};
    
    CGRect strRect = [self.text boundingRectWithSize:CGSizeMake(self.isAutoWidth ? MAXFLOAT : self.frame.size.width, self.isAutoHeight ? MAXFLOAT : self.frame.size.height) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:attrDic context:nil];
    
    return strRect.size.width;
}
-(CGFloat)getHeight {
    NSDictionary *attrDic = @{NSFontAttributeName: self.font ? self.font : [UIFont systemFontOfSize:13], NSForegroundColorAttributeName:self.textColor ? self.textColor : [UIColor blackColor]};
    
    CGRect strRect = [self.text boundingRectWithSize:CGSizeMake(self.isAutoWidth ? MAXFLOAT : self.frame.size.width, self.isAutoHeight ? MAXFLOAT : self.frame.size.height) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:attrDic context:nil];
    
    return strRect.size.height;
}
-(void)dealloc {
    self.text = @"";
}

@end
